/*
 * GYBuffer.h
 *
 *  Created on: 2015-9-25
 *      Author: gy
 */

#ifndef GYBUFFER_H_
#define GYBUFFER_H_

class GYBuffer
{
protected:
    int		m_iPos;	// 前两个成员必须是m_iPos, m_pszBuf, 和WSABUF保持一致
    char *	m_pszBuf;
    int		m_iSize;
    int		m_iAct;
    bool    m_bNew;
public:
    GYBuffer();

    GYBuffer(const GYBuffer &buf);

    virtual ~GYBuffer();
public:
    GYBuffer&		operator=(const GYBuffer &buf);
private:
    void			Copy(const GYBuffer &buf);
private:
    bool			operator==(const GYBuffer &buf);
public:
    int				Attach(char* pszBuf, int iSize, int iAct);
    void			Detach();

    int				Append(const char* pszBuf, int iLen);
    int				Append(int iLen);

    int				RemoveHead(int iLen);
    int				RemoveTail(int iLen);

    int				Clear();
public:
    char* GetUsedBuf() const
    {
        return m_pszBuf;
    }

    int GetUsedLen() const
    {
        return m_iPos;
    }

    char* GetFreeBuf() const
    {
        return m_pszBuf + m_iPos;
    }

    int GetFreeLen() const
    {
        return m_iSize - m_iPos;
    }

    int GetBufferSize() const
    {
        return m_iSize;
    }

    const int Act() const
    {
        return m_iAct;
    }

    int& Act()
    {
        return m_iAct;
    }

    bool IsAttached() const
    {
        return m_pszBuf != 0;
    }

    bool IsNewMemory() const
    {
        return m_bNew;
    }
};

#endif /* GYBUFFER_H_ */
